package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
public class SearchReceiver {

    @Autowired
    private SearchService searchService;

    /**
     * @author WeakMoon
     * @date 2025/02/26 11:13:12
     * 专辑上架
     * 该方法通过监听RabbitMQ队列来处理专辑的上架请求
     * 使用RabbitListener注解来监听特定的队列，当接收到消息时自动调用此方法
     * @param albumId 专辑ID，用于指定需要上架的专辑
     * @param message RabbitMQ的消息对象，包含消息的详细信息
     * @param channel RabbitMQ的通道对象，用于确认消息的接收
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),key = MqConst.ROUTING_ALBUM_UPPER))
    public void upperAlbum(Long albumId, Message message, Channel channel){
        try {
            // 检查专辑ID是否不为空，以防止无效的上架请求
            if (albumId != null){
                // 调用服务层方法
                searchService.upperAlbum(albumId);
            }
        } catch (Exception e) {
            // 记录消费过程中的异常信息
            log.error("消费异常" + e.getMessage());
            System.out.println(e.getMessage());
        }

        //手动确认签收
        try {
            // 确认消息接收成功，以便RabbitMQ可以删除该消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // 如果消息确认过程中发生异常，则抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    /**
     * @author WeakMoon
     * @date 2025/02/26 11:13:41
     * 专辑下架
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),key = MqConst.ROUTING_ALBUM_LOWER))
    public void lowerAlbum(Long albumId, Message message, Channel channel){
        // 检查专辑ID是否不为空
        try {
            if (albumId != null){
                // 调用搜索服务的专辑下架方法
                searchService.lowerAlbum(albumId);
            }
        } catch (Exception e) {
            // 记录消费异常日志
            log.error("消费异常" + e.getMessage());
            // 打印异常信息
            System.out.println(e.getMessage());
        }
        // 执行手动应答
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // 抛出运行时异常
            throw new RuntimeException(e);
        }
    }
}
